from . import schema
from common import cache
from fastapi import APIRouter, Depends, Security
from package.connector.sql_db import db
from package.fastapi import permission
from .view import ConfigureView


router_configure = APIRouter(prefix='/configure', tags=['系统配置'])
router_meta = {'module': '系统管理', 'sub_module': '系统配置'}


@router_configure.get('/login-option', response_model=schema.LoginOut,
                      openapi_extra=router_meta, name='login_option_get', summary='登陆选项获取')
def get_login(user=Security(permission.security, scopes=['login_option:read'])):
    return cache.configure_cache.get(name='login_option')


@router_configure.put('/login-option', response_model=schema.LoginOut,
                      openapi_extra=router_meta, name='login_option_put', summary='登陆选项修改')
def put_login(body: schema.Login,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['login_option:write'])):
    name = 'login_option'
    return ConfigureView.update_configure(session, name=name, update=body.dict())


@router_configure.get('/black-ip', response_model=schema.BlackIPOut,
                      openapi_extra=router_meta, name='black_ip_get', summary='地址黑名单获取')
def get_black(user=Security(permission.security, scopes=['black_ip:read'])):
    return cache.configure_cache.get(name='black_ip')


@router_configure.put('/black-ip', response_model=schema.BlackIPOut,
                      openapi_extra=router_meta, name='black_ip_put', summary='地址黑名单修改')
def put_black(body: schema.BlackIP,
              session=Depends(db.get_session),
              user=Security(permission.security, scopes=['black_ip:write'])):
    name = 'black_ip'
    return ConfigureView.update_configure(session, name=name, update=body.dict())


@router_configure.get('/disk-threshold', response_model=schema.DiskThresholdOut,
                      openapi_extra=router_meta, name='disk_threshold_get', summary='磁盘阈值获取')
def disk_threshold_get(user=Security(permission.security, scopes=['disk_threshold:read'])):
    return cache.configure_cache.get(name='disk_threshold')


@router_configure.put('/disk-threshold', response_model=schema.DiskThresholdOut,
                      openapi_extra=router_meta, name='disk_threshold_put', summary='磁盘阈值修改')
def disk_threshold_put(body: schema.DiskThreshold,
                       session=Depends(db.get_session),
                       user=Security(permission.security, scopes=['disk_threshold:write'])):
    name = 'disk_threshold'
    return ConfigureView.update_configure(session, name=name, update=body.dict())


@router_configure.get('/session-timeout', response_model=schema.SessionTimeoutOut,
                      openapi_extra=router_meta, name='session_timeout_get', summary='会话超时获取')
def session_timeout_get(user=Security(permission.security, scopes=['session_timeout:read'])):
    return cache.configure_cache.get(name='session_timeout')


@router_configure.put('/session-timeout', response_model=schema.SessionTimeoutOut,
                      openapi_extra=router_meta, name='session_timeout_put', summary='会话超时修改')
def session_timeout_put(body: schema.SessionTimeout,
                        session=Depends(db.get_session),
                        user=Security(permission.security, scopes=['session_timeout:write'])):
    name = 'session_timeout'
    return ConfigureView.update_configure(session, name=name, update=body.dict())


@router_configure.get('/role-map', response_model=schema.RoleMapOut,
                      openapi_extra=router_meta, name='role_map_get', summary='权限地图获取')
def role_map_get(user=Security(permission.security, scopes=['role_map:read'])):
    return cache.configure_cache.get(name='role_map')


@router_configure.put('/role-map', response_model=schema.RoleMapOut,
                      openapi_extra=router_meta, name='role_map_put', summary='权限地图修改')
def role_map_put(body: schema.RoleMap,
                 session=Depends(db.get_session),
                 user=Security(permission.security, scopes=['role_map:write'])):
    name = 'role_map'
    return ConfigureView.update_configure(session, name=name, update=body.dict())
